package cn.edu.bs.service.impl;

import cn.edu.bs.dao.mapper.BorrowMapper;
import cn.edu.bs.dao.mapper.custom.BorrowMapperCustom;
import cn.edu.bs.domain.Borrow;
import cn.edu.bs.domain.BorrowExample;
import cn.edu.bs.domain.custom.BorrowCustom;
import cn.edu.bs.service.BookService;
import cn.edu.bs.service.BorrowService;
import cn.edu.bs.service.UserService;
import cn.edu.bs.web.request.borrow.BorrowFormCommand;
import cn.edu.bs.web.vo.BorrowVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bs
 */
@Service("borrowService")
public class BorrowServiceImpl implements BorrowService {

    private final BorrowMapper borrowMapper;

    private final BorrowMapperCustom borrowMapperCustom;

    private final UserService userService;

    private final BookService bookService;

    @Autowired
    public BorrowServiceImpl(BorrowMapper borrowMapper, @Qualifier("borrowMapperCustom") BorrowMapperCustom borrowMapperCustom, UserService userService, BookService bookService) {
        this.borrowMapper = borrowMapper;
        this.borrowMapperCustom = borrowMapperCustom;
        this.userService = userService;
        this.bookService = bookService;
    }

    @Override
    public long countByExample(BorrowExample example) {
        return borrowMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(BorrowExample example) {
        return borrowMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return borrowMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Borrow record) {
        return borrowMapper.insert(record);
    }

    @Override
    public int insertSelective(Borrow record) {
        return borrowMapper.insertSelective(record);
    }

    @Override
    public List<Borrow> selectByExample(BorrowExample example) {
        return borrowMapper.selectByExample(example);
    }

    @Override
    public Borrow selectByPrimaryKey(Integer id) {
        return borrowMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(Borrow record, BorrowExample example) {
        return borrowMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(Borrow record, BorrowExample example) {
        return borrowMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(Borrow record) {
        return borrowMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Borrow record) {
        return borrowMapper.updateByPrimaryKey(record);
    }

    @Override
    public Borrow transformCommandToEntity(BorrowFormCommand command) {
        Borrow borrow = new Borrow();
        BeanUtils.copyProperties(command, borrow);
        return borrow;
    }

    @Override
    public BorrowVO transformEntityToVO(Borrow entity) {
        BorrowVO vo = new BorrowVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public BorrowVO transformCustomToVO(BorrowCustom custom) {
        BorrowVO vo = new BorrowVO();
        BeanUtils.copyProperties(custom, vo);
        if (custom.getUserCustom() != null) {
            vo.setUserVO(userService.transformEntityToVO(custom.getUserCustom()));
        }
        if (custom.getBookCustom() != null) {
            vo.setBookVO(bookService.transformEntityToVO(custom.getBookCustom()));
        }
        return vo;
    }

    @Override
    public List<BorrowVO> transformEntitiesToTOs(List<Borrow> entities) {
        List<BorrowVO> vos = new ArrayList<>();
        for (Borrow borrow : entities) {
            vos.add(transformEntityToVO(borrow));
        }
        return vos;
    }

    @Override
    public List<BorrowVO> transformCustomsToTOs(List<BorrowCustom> customs) {
        List<BorrowVO> vos = new ArrayList<>();
        for (BorrowCustom custom : customs) {
            vos.add(transformCustomToVO(custom));
        }
        return vos;
    }

    @Override
    public List<BorrowCustom> selectByUserId(Integer userId) {
        return borrowMapperCustom.selectByUserId(userId);
    }

    @Override
    public List<BorrowCustom> selectBorrowList() {
        return borrowMapperCustom.selectBorrowList();
    }
}
